##########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script merges the party mention data and 
#             runs the logit regression
# Author:     Jan Schwalbach (21/07/2022)
##########################################################

# Loading packages and data

library(quanteda)
library(ggrepel)
library(grid)
library(zoo)
library(pscl)
library(aod)
library(ggplot2)
library(stringr)
library(MASS)
library(mfx)
library(erer)
library(tibble)
library(broom)
library(margins)
library(Ecdat)
library(dplyr)
library(stargazer)
library(sjPlot)
library(sjlabelled)
library(sjmisc)
library(ggplot2)
library(ggstatsplot)
theme_set(theme_sjplot())
theme_sjplot(base_size = 12, base_family = "Times")
library(extrafont)
font_import()
loadfonts(device="win") 

# Loading and merging data sets and renaming variables for the regression

load(file="./Denmark_party_mentions.Rdata")
load(file="./Germany_party_mentions.Rdata")
load(file="./Netherlands_party_mentions.Rdata")
load(file="./Sweden_party_mentions.Rdata")

allcountries <- rbind(Germany, Sweden, Denmark, Netherlands)
allcountries$Government_Debate <- allcountries$type
allcountries$Government_Debate[allcountries$Government_Debate == "government"] <- 1
allcountries$Government_Debate[allcountries$Government_Debate == "opposition"] <- 0
allcountries$Government_Debate <- as.numeric(allcountries$Government_Debate)
allcountries$PRR_Size <- allcountries$RRP_size
allcountries$Left_Party <- allcountries$left
allcountries$Education_Debate <- allcountries$education
allcountries$Immigration_Debate <- allcountries$immigration
allcountries$Term <- allcountries$term
allcountries$PRR_Support <- allcountries$support
allcountries$Country <- allcountries$country

### Logit regression model

logit_model <- glm(RWdummy ~ Left_Party + Government + PRR_Size + minority + Immigration_Debate + Education_Debate + Government_Debate + PRR_Support + Term  + Country, data = allcountries, family = "binomial")
summary(logit_model)

stargazer(logit_model, star.cutoffs = c(0.05, 0.01, 0.001))


stargazer(logit_model, type = "html", out="models1.html", star.cutoffs = c(0.05, 0.01, 0.001))

# Plotting the figure 2

plot_model(logit_model, vline.color = "black", colors = "Set1", transform = "plogis", title = "", 
           axis.title = "Predicted Probabilities",
           axis.labels = c("Sweden","Netherlands","Germany"
           ,"Term Number","PRRP Support"
           ,"Government Debate","Education Debate","Immigration Debate","Minority"
           ,"PRRP Size","Opposition Party","Left Party"))+theme_sjplot(base_size = 16, base_family = "TT Times New Roman")
